/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.U.
 */
package es.silenus.detecta.controller;

import es.silenus.detecta.dao.CouncilDAO;
import es.silenus.detecta.dao.ProvinceDAO;

import es.silenus.detecta.model.Council;
import es.silenus.detecta.model.Province;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * The province controller.
 *
 * @author $author$
 * @version $Revision$
 *
 * @since $Date$
 */
public class ProvinceController extends BaseController {
	/**
	 * The province DAO.
	 */
	private ProvinceDAO provinceDAO;

	/**
	 * The council DAO.
	 */
	private CouncilDAO councilDAO;

	/**
	 * The default province id.
	 */
	private String defaultProvinceId;

/**
   * Constructor.
   */
	public ProvinceController() {
	}

	/**
	 * Sets the province DAO.
	 *
	 * @param provinceDAO the province DAO.
	 */
	public void setProvinceDAO(final ProvinceDAO provinceDAO) {
		this.provinceDAO = provinceDAO;
	}

	/**
	 * Sets the council DAO.
	 *
	 * @param councilDAO the council DAO.
	 */
	public void setCouncilDAO(final CouncilDAO councilDAO) {
		this.councilDAO = councilDAO;
	}

	/**
	 * Sets the default province id.
	 *
	 * @param defaultProvinceId the default province id.
	 */
	public void setDefaultProvinceId(final String defaultProvinceId) {
		this.defaultProvinceId = defaultProvinceId;
	}

	/**
	 * Get council List.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String getProvinceList(final HttpServletRequest request, final HttpServletResponse response) {
		List<Province> provinces = provinceDAO.findAll();

		request.setAttribute("items", provinces);

		return "province";
	}

	/**
	 * Retrieves councils by its province.
	 *
	 * @param request the request.
	 * @param response the response.
	 *
	 * @return the view name.
	 */
	public String getCouncilByProvinceId(final HttpServletRequest request, final HttpServletResponse response) {
		String id = request.getParameter("id");

		id = ((id != null) && (id.length() > 0)) ? id : defaultProvinceId;

		List<Council> councils = councilDAO.findByProvince(id);

		request.setAttribute("items", councils);

		return "council";
	}
}
